﻿namespace UI
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using Common.Protocol;

    public partial class SwitcherCell : UserControl
    {
        private bool isOn = false;

        public SwitcherCell()
        {
            this.InitializeComponent();
        }

        public void Initialize(Channel channel)
        {
            this.colorPanel.BackColor = channel.Color;
            this.nameLabel.Text = channel.Id;
            if (channel.Name != string.Empty && channel.Name.Trim(new char[] { ' ' }) != string.Empty)
            {
                ToolTip toolTip = new ToolTip();
                toolTip.AutoPopDelay = 10000;
                toolTip.ReshowDelay = 100;
                toolTip.InitialDelay = 100;
                toolTip.SetToolTip(this.nameLabel, channel.Name);
            }
        }

        public bool IsOn
        {
            get
            {
                return this.isOn;
            }

            set
            {
                this.isOn = value;
                if (this.isOn)
                {
                    this.nameLabel.BackColor = Color.LightGreen;
                }
                else
                {
                    this.nameLabel.BackColor = Color.White;
                }
            }
        }

        public new string Text
        {
            get
            {
                return this.nameLabel.Text;
            }

            set
            {
                this.nameLabel.Text = value;
            }
        }

        public void Disable()
        {
            if (this.Enabled)
            {
                this.Enabled = false;
                this.nameLabel.BackColor = Color.White;
                this.colorPanel.BackColor = SystemColors.ActiveCaption;
            }
        }

        private void SwitcherCellResizeEventHandler(object sender, EventArgs e)
        {
            this.nameLabel.Left = this.colorPanel.Right;
            this.nameLabel.Top = 0;
            this.nameLabel.Width = this.Width - this.colorPanel.Width;
            this.nameLabel.Height = this.Height;
        }

        private void SwitcherCellSizeChangedEventHandler(object sender, EventArgs e)
        {
            this.nameLabel.Left = this.colorPanel.Right;
            this.nameLabel.Top = 0;
            this.nameLabel.Width = this.Width - this.colorPanel.Width;
            this.nameLabel.Height = this.Height;
        }

        private void IdLabelMouseClickEventHandler(object sender, MouseEventArgs e)
        {
            this.OnClick(e);
        }
    }
}